{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Trainer\n",
    "千帆Python SDK 在使用[resource API实现发起训练微调](./api_based_finetune.ipynb)之外，还提供了Trainer API，可以更方便地实现一体化的训练微调pipeline。同时提供了状态事件回调函数的注册，通过事件分发实现训练流程状态事件的监控。\n",
    "\n",
    "\n",
    "本例将基于qianfan==0.2.2展示通过Dataset加载本地数据集，并上传到千帆平台，基于ERNIE-Bot-turbo进行fine-tune，并使用Model进行批量跑评估数据，直到最终完成服务发布，并最终实现服务调用的完整过程。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "! pip install \"qianfan>=0.2.8\" -U"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'0.2.7'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import qianfan\n",
    "qianfan.__version__"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 前置准备\n",
    "- 初始化千帆安全认证AK、SK"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os \n",
    "\n",
    "os.environ[\"QIANFAN_ACCESS_KEY\"] = \"your_ak\"\n",
    "os.environ[\"QIANFAN_SECRET_KEY\"] = \"your_sk\""
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 导入依赖\n",
    "- `qianfan.trainer.consts` trainer使用中所用到的常量\n",
    "- `qianfan.resources.console.consts` api层面定义的字段常量\n",
    "- `qianfan.trainer.configs` trainer使用所需要的config配置数据类\n",
    "- `qianfan.resources.QfMessages` 用于组装qianfan.ChatCompletion的输入messages\n",
    "- `qianfan.trainer.LLMFinetune` 大语言模型fine-tune任务Trainer实现\n",
    "- `qianfan.dataset.Dataset` 千帆dataset类，用于管理千帆平台、本地、第三方数据集的导入导出，数据清洗等操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from qianfan.trainer.consts import ActionState\n",
    "from qianfan.model.consts import ServiceType\n",
    "from qianfan.resources.console import consts as console_consts\n",
    "from qianfan.trainer.configs import TrainConfig\n",
    "from qianfan.model.configs import DeployConfig\n",
    "from qianfan.resources import QfMessages\n",
    "from qianfan.trainer import LLMFinetune\n",
    "from qianfan.dataset import Dataset\n",
    "from qianfan.utils import enable_log\n",
    "import logging\n",
    "\n",
    "enable_log(logging.INFO)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数据集加载\n",
    "\n",
    "千帆SDK提供了数据集实现帮助我们可以快速的加载本地的数据集到内存，并通过设定DataSource数据源以保存至本地和千帆平台。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[{'prompt': '下文中市场价格下降导致市场价格下降事件对应的结果涉及的产品是？在国际奶粉价格下降压力下,国内奶价仍有下降空间',\n",
       "   'response': [['奶']]}],\n",
       " [{'prompt': '下文中市场价格下降导致市场价格下降事件对应的结果涉及的地区是？在国际奶粉价格下降压力下,国内奶价仍有下降空间',\n",
       "   'response': [['国内']]}],\n",
       " [{'prompt': '下文中有哪些因果事件？随着供应缩减，需求回升，高碳铬铁价格企稳回升，预期短期内或仍维持偏强态势，但高碳铬铁产能依然偏向过剩，后期价格弹升空间有限',\n",
       "   'response': [['需求增加导致市场价格提升']]}],\n",
       " [{'prompt': '下文中需求增加导致市场价格提升事件对应的原因涉及的产品是？随着供应缩减，需求回升，高碳铬铁价格企稳回升，预期短期内或仍维持偏强态势，但高碳铬铁产能依然偏向过剩，后期价格弹升空间有限',\n",
       "   'response': [['高碳铬铁']]}],\n",
       " [{'prompt': '下文中需求增加导致市场价格提升事件对应的原因涉及的行业是？随着供应缩减，需求回升，高碳铬铁价格企稳回升，预期短期内或仍维持偏强态势，但高碳铬铁产能依然偏向过剩，后期价格弹升空间有限',\n",
       "   'response': [['我们无法得知，可能需要更多内容说明。']]}],\n",
       " [{'prompt': '下文中需求增加导致市场价格提升事件对应的结果涉及的产品是？随着供应缩减，需求回升，高碳铬铁价格企稳回升，预期短期内或仍维持偏强态势，但高碳铬铁产能依然偏向过剩，后期价格弹升空间有限',\n",
       "   'response': [['高碳铬铁']]}],\n",
       " [{'prompt': '下文中有哪些因果事件？但由于7-ACA市场价格大幅下跌，跌幅超过50%，导致公司相关原料药销售价格随之下跌',\n",
       "   'response': [['市场价格下降导致市场价格下降']]}],\n",
       " [{'prompt': '下文中市场价格下降导致市场价格下降事件对应的原因涉及的产品是？但由于7-ACA市场价格大幅下跌，跌幅超过50%，导致公司相关原料药销售价格随之下跌',\n",
       "   'response': [['7-ACA']]}],\n",
       " [{'prompt': '下文中市场价格下降导致市场价格下降事件对应的结果涉及的产品是？但由于7-ACA市场价格大幅下跌，跌幅超过50%，导致公司相关原料药销售价格随之下跌',\n",
       "   'response': [['原料药']]}],\n",
       " [{'prompt': '下文中市场价格下降导致市场价格下降事件对应的结果涉及的行业是？但由于7-ACA市场价格大幅下跌，跌幅超过50%，导致公司相关原料药销售价格随之下跌',\n",
       "   'response': [['暂不清楚，需要更多信息说明。']]}],\n",
       " [{'prompt': '下文中有哪些因果事件？寒潮导致农产品价格继续攀升', 'response': [['寒潮导致市场价格提升']]}],\n",
       " [{'prompt': '下文中寒潮导致市场价格提升事件对应的结果涉及的产品是？寒潮导致农产品价格继续攀升',\n",
       "   'response': [['农产品']]}],\n",
       " [{'prompt': '下文中有哪些因果事件？而发改委分别在5月10日和6月9日对国内汽柴油价格进行了下调，汽油和柴油零售价格分别累计下调了860和820元/吨，导致石化双雄（尤其是中石化）不仅炼油损逐月扩大、成品油销售受阻并且出现库存跌价',\n",
       "   'response': [['市场价格下降导致销量（消费）减少']]}],\n",
       " [{'prompt': '下文中市场价格下降导致销量（消费）减少事件对应的原因涉及的产品是？而发改委分别在5月10日和6月9日对国内汽柴油价格进行了下调，汽油和柴油零售价格分别累计下调了860和820元/吨，导致石化双雄（尤其是中石化）不仅炼油损逐月扩大、成品油销售受阻并且出现库存跌价',\n",
       "   'response': [['汽油,柴油']]}],\n",
       " [{'prompt': '下文中市场价格下降导致销量（消费）减少事件对应的结果涉及的产品是？而发改委分别在5月10日和6月9日对国内汽柴油价格进行了下调，汽油和柴油零售价格分别累计下调了860和820元/吨，导致石化双雄（尤其是中石化）不仅炼油损逐月扩大、成品油销售受阻并且出现库存跌价',\n",
       "   'response': [['成品油']]}],\n",
       " [{'prompt': '下文中市场价格下降导致销量（消费）减少事件对应的结果涉及的地区是？而发改委分别在5月10日和6月9日对国内汽柴油价格进行了下调，汽油和柴油零售价格分别累计下调了860和820元/吨，导致石化双雄（尤其是中石化）不仅炼油损逐月扩大、成品油销售受阻并且出现库存跌价',\n",
       "   'response': [['从以上文本中我们暂无发现。']]}],\n",
       " [{'prompt': '下文中有哪些因果事件？利润和每股收益大幅下降的主要原因是煤炭价格大幅上涨，导致公司投资的火电企业的煤炭成本大幅增加###但报告期内受煤炭行业去产能改革的影响，公司煤炭前三季度平均采购价格同比上涨44.53%，导致成本涨幅抵消了销售价格的涨幅',\n",
       "   'response': [['市场价格提升导致运营成本提升']]}],\n",
       " [{'prompt': '下文中市场价格提升导致运营成本提升事件对应的原因涉及的产品是？利润和每股收益大幅下降的主要原因是煤炭价格大幅上涨，导致公司投资的火电企业的煤炭成本大幅增加###但报告期内受煤炭行业去产能改革的影响，公司煤炭前三季度平均采购价格同比上涨44.53%，导致成本涨幅抵消了销售价格的涨幅',\n",
       "   'response': [['煤炭']]}],\n",
       " [{'prompt': '下文中市场价格提升导致运营成本提升事件对应的原因涉及的行业是？利润和每股收益大幅下降的主要原因是煤炭价格大幅上涨，导致公司投资的火电企业的煤炭成本大幅增加###但报告期内受煤炭行业去产能改革的影响，公司煤炭前三季度平均采购价格同比上涨44.53%，导致成本涨幅抵消了销售价格的涨幅',\n",
       "   'response': [['以上内容暂无说明。']]}],\n",
       " [{'prompt': '下文中市场价格提升导致运营成本提升事件对应的结果涉及的产品是？利润和每股收益大幅下降的主要原因是煤炭价格大幅上涨，导致公司投资的火电企业的煤炭成本大幅增加###但报告期内受煤炭行业去产能改革的影响，公司煤炭前三季度平均采购价格同比上涨44.53%，导致成本涨幅抵消了销售价格的涨幅',\n",
       "   'response': [['煤炭']]}],\n",
       " [{'prompt': '下文中有哪些因果事件？风险因素:国内外因天气等自然灾害导致用肥量锐减',\n",
       "   'response': [['其他自然灾害导致需求减少']]}],\n",
       " [{'prompt': '下文中其他自然灾害导致需求减少事件对应的原因涉及的产品是？风险因素:国内外因天气等自然灾害导致用肥量锐减',\n",
       "   'response': [['以上内容暂无说明。']]}],\n",
       " [{'prompt': '下文中其他自然灾害导致需求减少事件对应的原因涉及的地区是？风险因素:国内外因天气等自然灾害导致用肥量锐减',\n",
       "   'response': [['国内外']]}],\n",
       " [{'prompt': '下文中其他自然灾害导致需求减少事件对应的结果涉及的产品是？风险因素:国内外因天气等自然灾害导致用肥量锐减',\n",
       "   'response': [['肥']]}],\n",
       " [{'prompt': '下文中有哪些因果事件？东南亚棕榈油减产,原油价格大幅上涨###东南亚棕榈油减产,原油价格大幅上涨###东南亚棕榈油减产，原油价格大幅上涨###东南亚棕榈油减产，原油价格大幅上涨###东南亚棕榈油减产,原油价格大幅上涨###东南亚棕榈油减产，原油价格大幅上涨###东南亚棕榈油减产,原油价格大幅上涨###东南亚棕榈油减产,原油价格大幅上涨###东南亚棕榈油减产,原油价格大幅上涨',\n",
       "   'response': [['供给减少导致市场价格提升']]}],\n",
       " [{'prompt': '下文中供给减少导致市场价格提升事件对应的原因涉及的产品是？东南亚棕榈油减产,原油价格大幅上涨###东南亚棕榈油减产,原油价格大幅上涨###东南亚棕榈油减产，原油价格大幅上涨###东南亚棕榈油减产，原油价格大幅上涨###东南亚棕榈油减产,原油价格大幅上涨###东南亚棕榈油减产，原油价格大幅上涨###东南亚棕榈油减产,原油价格大幅上涨###东南亚棕榈油减产,原油价格大幅上涨###东南亚棕榈油减产,原油价格大幅上涨',\n",
       "   'response': [['棕榈油']]}],\n",
       " [{'prompt': '下文中供给减少导致市场价格提升事件对应的原因涉及的地区是？东南亚棕榈油减产,原油价格大幅上涨###东南亚棕榈油减产,原油价格大幅上涨###东南亚棕榈油减产，原油价格大幅上涨###东南亚棕榈油减产，原油价格大幅上涨###东南亚棕榈油减产,原油价格大幅上涨###东南亚棕榈油减产，原油价格大幅上涨###东南亚棕榈油减产,原油价格大幅上涨###东南亚棕榈油减产,原油价格大幅上涨###东南亚棕榈油减产,原油价格大幅上涨',\n",
       "   'response': [['东南亚']]}],\n",
       " [{'prompt': '下文中供给减少导致市场价格提升事件对应的结果涉及的产品是？东南亚棕榈油减产,原油价格大幅上涨###东南亚棕榈油减产,原油价格大幅上涨###东南亚棕榈油减产，原油价格大幅上涨###东南亚棕榈油减产，原油价格大幅上涨###东南亚棕榈油减产,原油价格大幅上涨###东南亚棕榈油减产，原油价格大幅上涨###东南亚棕榈油减产,原油价格大幅上涨###东南亚棕榈油减产,原油价格大幅上涨###东南亚棕榈油减产,原油价格大幅上涨',\n",
       "   'response': [['原油']]}],\n",
       " [{'prompt': '下文中有哪些因果事件？南美大豆供求宽松对豆类油脂价格利空', 'response': [['供给增加导致负向影响']]}],\n",
       " [{'prompt': '下文中供给增加导致负向影响事件对应的原因涉及的产品是？南美大豆供求宽松对豆类油脂价格利空',\n",
       "   'response': [['大豆']]}],\n",
       " [{'prompt': '下文中供给增加导致负向影响事件对应的原因涉及的地区是？南美大豆供求宽松对豆类油脂价格利空',\n",
       "   'response': [['南美']]}],\n",
       " [{'prompt': '下文中供给增加导致负向影响事件对应的结果涉及的产品是？南美大豆供求宽松对豆类油脂价格利空',\n",
       "   'response': [['豆类油脂']]}]]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from qianfan.dataset import Dataset\n",
    "\n",
    "# 加载本地数据集\n",
    "ds: Dataset = Dataset.load(data_file=\"./data/fin_cqa_train.jsonl\")\n",
    "ds.list()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "从本地数据集上传到BOS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[INFO] [12-08 18:23:51] data_source.py:749 [t:140487811458880]: start to create dataset on qianfan\n",
      "[INFO] [12-08 18:23:52] data_source.py:767 [t:140487811458880]: create dataset on qianfan successfully\n",
      "[INFO] [12-08 18:23:52] schema.py:30 [t:140487811458880]: unpack dataset before validating\n",
      "[INFO] [12-08 18:23:52] dataset.py:817 [t:140487811458880]: list local dataset data by 0\n",
      "[INFO] [12-08 18:23:52] schema.py:33 [t:140487811458880]: pack dataset after validation\n",
      "[INFO] [12-08 18:23:52] dataset.py:171 [t:140487811458880]: export as format: FormatType.Jsonl\n",
      "[INFO] [12-08 18:23:52] dataset.py:183 [t:140487811458880]: enter packed deserialization logic\n",
      "[INFO] [12-08 18:23:52] data_source.py:448 [t:140487811458880]: start to upload data to user BOS\n",
      "[INFO] [12-08 18:23:52] data_source.py:461 [t:140487811458880]: uploading data to user BOS finished\n",
      "[INFO] [12-08 18:23:54] data_source.py:294 [t:140487811458880]: successfully create importing task\n",
      "[INFO] [12-08 18:23:56] data_source.py:297 [t:140487811458880]: polling import task status\n",
      "[INFO] [12-08 18:23:56] data_source.py:304 [t:140487811458880]: import status: 1, keep polling\n",
      "[INFO] [12-08 18:23:58] data_source.py:297 [t:140487811458880]: polling import task status\n",
      "[INFO] [12-08 18:23:59] data_source.py:304 [t:140487811458880]: import status: 1, keep polling\n",
      "[INFO] [12-08 18:24:01] data_source.py:297 [t:140487811458880]: polling import task status\n",
      "[INFO] [12-08 18:24:03] data_source.py:304 [t:140487811458880]: import status: 1, keep polling\n",
      "[INFO] [12-08 18:24:05] data_source.py:297 [t:140487811458880]: polling import task status\n",
      "[INFO] [12-08 18:24:06] data_source.py:307 [t:140487811458880]: import succeed\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 保存到千帆平台\n",
    "from qianfan.dataset.data_source import QianfanDataSource\n",
    "from qianfan.resources.console import consts as console_consts\n",
    "\n",
    "bos_bucket_name = \"sdk-test\"\n",
    "bos_bucket_file_path = \"/sdk_ds/\"\n",
    "\n",
    "\n",
    "# 创建千帆数据集，并上传保存\n",
    "qianfan_data_source = QianfanDataSource.create_bare_dataset(\n",
    "    name=\"sdk_trainer_ds_022z\",\n",
    "    template_type=console_consts.DataTemplateType.NonSortedConversation,\n",
    "    storage_type=console_consts.DataStorageType.PrivateBos,\n",
    "    storage_id=bos_bucket_name,\n",
    "    storage_path=bos_bucket_file_path,\n",
    ")\n",
    "\n",
    "ds.save(qianfan_data_source, replace_source=True)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### LLMFinetune 训练\n",
    "`LLMFinetune` 实现了SFT逻辑的trainer，它内部组装了SFT所需要的基本`Pipeline`, 用于串联数据->训练->模型发布->服务调用等步骤"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "from qianfan.trainer.consts import PeftType\n",
    "\n",
    "trainer = LLMFinetune(\n",
    "    train_type=\"ERNIE-Speed\",\n",
    "    train_config=TrainConfig(\n",
    "        epoch=1,\n",
    "        learning_rate=0.0003,\n",
    "        max_seq_len=4096,\n",
    "        peft_type=PeftType.LoRA,\n",
    "        logging_steps=1,\n",
    "        warmup_ratio=0.10,\n",
    "        weight_decay=0.0100,\n",
    "        lora_rank=8,\n",
    "        lora_all_linear=\"True\",\n",
    "    ),\n",
    "    dataset=ds,\n",
    ")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 运行任务\n",
    "同步运行trainer，训练直到模型发布完成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[INFO] [12-08 11:40:06] data_source.py:1044 [t:140481718310720]: data releasing, keep rolling\n",
      "[INFO] [12-08 11:40:12] data_source.py:1044 [t:140481718310720]: data releasing, keep rolling\n",
      "[INFO] [12-08 11:40:15] data_source.py:1044 [t:140481718310720]: data releasing, keep rolling\n",
      "[INFO] [12-08 11:40:18] data_source.py:1044 [t:140481718310720]: data releasing, keep rolling\n",
      "[INFO] [12-08 11:40:22] data_source.py:1053 [t:140481718310720]: data releasing succeeded\n",
      "[INFO] [12-08 11:40:25] actions.py:352 [t:140481718310720]: [train_action] fine-tune running... current status: RUNNING, check vdl report in https://console.bce.baidu.com/qianfan/visualdl/index?displayToken=eyJydW5JZCI6InJ1bi1kdjlqYjNnMW13NWkxdWRqIn0=\n",
      "[INFO] [12-08 11:40:56] actions.py:352 [t:140481718310720]: [train_action] fine-tune running... current status: RUNNING, check vdl report in https://console.bce.baidu.com/qianfan/visualdl/index?displayToken=eyJydW5JZCI6InJ1bi1kdjlqYjNnMW13NWkxdWRqIn0=\n",
      "[INFO] [12-08 11:41:26] actions.py:352 [t:140481718310720]: [train_action] fine-tune running... current status: RUNNING, check vdl report in https://console.bce.baidu.com/qianfan/visualdl/index?displayToken=eyJydW5JZCI6InJ1bi1kdjlqYjNnMW13NWkxdWRqIn0=\n",
      "[INFO] [12-08 11:41:57] actions.py:352 [t:140481718310720]: [train_action] fine-tune running... current status: RUNNING, check vdl report in https://console.bce.baidu.com/qianfan/visualdl/index?displayToken=eyJydW5JZCI6InJ1bi1kdjlqYjNnMW13NWkxdWRqIn0=\n",
      "[INFO] [12-08 11:42:28] actions.py:352 [t:140481718310720]: [train_action] fine-tune running... current status: RUNNING, check vdl report in https://console.bce.baidu.com/qianfan/visualdl/index?displayToken=eyJydW5JZCI6InJ1bi1kdjlqYjNnMW13NWkxdWRqIn0=\n",
      "[INFO] [12-08 11:42:58] actions.py:352 [t:140481718310720]: [train_action] fine-tune running... current status: RUNNING, check vdl report in https://console.bce.baidu.com/qianfan/visualdl/index?displayToken=eyJydW5JZCI6InJ1bi1kdjlqYjNnMW13NWkxdWRqIn0=\n",
      "[INFO] [12-08 12:05:02] actions.py:352 [t:140481718310720]: [train_action] fine-tune running... current status: RUNNING, check vdl report in https://console.bce.baidu.com/qianfan/visualdl/index?displayToken=eyJydW5JZCI6InJ1bi1kdjlqYjNnMW13NWkxdWRqIn0=\n",
      "[INFO] [12-08 12:05:33] actions.py:352 [t:140481718310720]: [train_action] fine-tune running... current status: RUNNING, check vdl report in https://console.bce.baidu.com/qianfan/visualdl/index?displayToken=eyJydW5JZCI6InJ1bi1kdjlqYjNnMW13NWkxdWRqIn0=\n",
      "[INFO] [12-08 12:06:04] actions.py:352 [t:140481718310720]: [train_action] fine-tune running... current status: RUNNING, check vdl report in https://console.bce.baidu.com/qianfan/visualdl/index?displayToken=eyJydW5JZCI6InJ1bi1kdjlqYjNnMW13NWkxdWRqIn0=\n",
      "[INFO] [12-08 12:06:35] actions.py:352 [t:140481718310720]: [train_action] fine-tune running... current status: RUNNING, check vdl report in https://console.bce.baidu.com/qianfan/visualdl/index?displayToken=eyJydW5JZCI6InJ1bi1kdjlqYjNnMW13NWkxdWRqIn0=\n",
      "[INFO] [12-08 12:07:06] actions.py:352 [t:140481718310720]: [train_action] fine-tune running... current status: RUNNING, check vdl report in https://console.bce.baidu.com/qianfan/visualdl/index?displayToken=eyJydW5JZCI6InJ1bi1kdjlqYjNnMW13NWkxdWRqIn0=\n",
      "[INFO] [12-08 12:07:37] actions.py:352 [t:140481718310720]: [train_action] fine-tune running... current status: RUNNING, check vdl report in https://console.bce.baidu.com/qianfan/visualdl/index?displayToken=eyJydW5JZCI6InJ1bi1kdjlqYjNnMW13NWkxdWRqIn0=\n",
      "[INFO] [12-08 12:08:08] actions.py:352 [t:140481718310720]: [train_action] fine-tune running... current status: RUNNING, check vdl report in https://console.bce.baidu.com/qianfan/visualdl/index?displayToken=eyJydW5JZCI6InJ1bi1kdjlqYjNnMW13NWkxdWRqIn0=\n",
      "[INFO] [12-08 12:08:38] actions.py:352 [t:140481718310720]: [train_action] fine-tune running... current status: FINISH, check vdl report in https://console.bce.baidu.com/qianfan/visualdl/index?displayToken=eyJydW5JZCI6InJ1bi1kdjlqYjNnMW13NWkxdWRqIn0=\n",
      "[INFO] [12-08 12:08:38] actions.py:370 [t:140481718310720]: [train_action] fine-tune job has ended: 9098 with status: FINISH\n",
      "[INFO] [12-08 12:08:39] model.py:183 [t:140481718310720]: check train job: 17320/9098 status before publishing model\n",
      "[INFO] [12-08 12:08:39] model.py:198 [t:140481718310720]: model publishing keep polling, current status FINISH\n",
      "[INFO] [12-08 12:08:40] model.py:232 [t:140481718310720]: model ready to publish\n",
      "[INFO] [12-08 12:08:40] model.py:238 [t:140481718310720]: check model publish status: Creating\n",
      "[INFO] [12-08 12:09:11] model.py:238 [t:140481718310720]: check model publish status: Ready\n",
      "[INFO] [12-08 12:09:11] model.py:240 [t:140481718310720]: model 10268/12722 published successfully\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<qianfan.trainer.finetune.LLMFinetune at 0x7fc40aa51850>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "trainer.run()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "获取finetune任务输出："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'task_id': 17320,\n",
       " 'job_id': 9098,\n",
       " 'model_id': 'am-cmyddmcb0b1e',\n",
       " 'model_version_id': 'amv-7598fwf39uf2',\n",
       " 'model': <qianfan.trainer.model.Model at 0x7fc40ac23ad0>}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "trainer.output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'10268'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "o[\"model_id\"]"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 运行批量评估推理\n",
    "Model支持模型批量运行评估数据集，并保存到千帆平台"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "调用Fine-tune得到的Model对象的`batch_run_on_qianfan`发起批量任务，这可能会持续数十分钟"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[INFO] [12-08 12:22:22] model.py:295 [t:140481718310720]: start to create evaluation task in model\n",
      "[INFO] [12-08 12:22:25] model.py:319 [t:140481718310720]: start to polling status of evaluation task 2419\n",
      "[INFO] [12-08 12:22:26] model.py:326 [t:140481718310720]: current eval_state: Doing\n",
      "[INFO] [12-08 12:22:57] model.py:326 [t:140481718310720]: current eval_state: Doing\n",
      "[INFO] [12-08 12:23:27] model.py:326 [t:140481718310720]: current eval_state: Doing\n",
      "[INFO] [12-08 12:38:42] model.py:326 [t:140481718310720]: current eval_state: DoingWithManualBegin\n",
      "[INFO] [12-08 12:38:42] model.py:344 [t:140481718310720]: get result dataset id 39815\n",
      "[INFO] [12-08 12:38:42] dataset.py:353 [t:140481718310720]: no data source was provided, construct\n",
      "[INFO] [12-08 12:38:42] dataset.py:255 [t:140481718310720]: construct a qianfan data source from existed id: 39815, with args: {}\n",
      "[INFO] [12-08 12:38:43] data_source.py:1022 [t:140481718310720]: start to fetch dataset cache because is_download_to_local is set\n",
      "[INFO] [12-08 12:38:44] data_source.py:635 [t:140481718310720]: no cache was found, download cache\n",
      "[INFO] [12-08 12:38:44] data_source.py:525 [t:140481718310720]: get dataset info succeeded for dataset id 39815\n",
      "[INFO] [12-08 12:38:44] data_source.py:533 [t:140481718310720]: start to export dataset\n",
      "[INFO] [12-08 12:38:45] data_source.py:537 [t:140481718310720]: create dataset export task successfully\n",
      "[INFO] [12-08 12:38:47] data_source.py:542 [t:140481718310720]: polling export task status\n",
      "[INFO] [12-08 12:38:48] data_source.py:550 [t:140481718310720]: export status: 1, keep polling\n",
      "[INFO] [12-08 12:38:50] data_source.py:542 [t:140481718310720]: polling export task status\n",
      "[INFO] [12-08 12:38:53] data_source.py:550 [t:140481718310720]: export status: 1, keep polling\n",
      "[INFO] [12-08 12:38:55] data_source.py:542 [t:140481718310720]: polling export task status\n",
      "[INFO] [12-08 12:38:55] data_source.py:550 [t:140481718310720]: export status: 1, keep polling\n",
      "[INFO] [12-08 12:38:57] data_source.py:542 [t:140481718310720]: polling export task status\n",
      "[INFO] [12-08 12:38:58] data_source.py:550 [t:140481718310720]: export status: 1, keep polling\n",
      "[INFO] [12-08 12:39:00] data_source.py:542 [t:140481718310720]: polling export task status\n",
      "[INFO] [12-08 12:39:02] data_source.py:550 [t:140481718310720]: export status: 1, keep polling\n",
      "[INFO] [12-08 12:39:04] data_source.py:542 [t:140481718310720]: polling export task status\n",
      "[INFO] [12-08 12:39:05] data_source.py:547 [t:140481718310720]: export succeed\n",
      "[INFO] [12-08 12:39:06] data_source.py:498 [t:140481718310720]: get export records succeeded for dataset id 39815\n",
      "[INFO] [12-08 12:39:06] data_source.py:514 [t:140481718310720]: latest dataset with time2023-12-08 12:39:02 for dataset id 12100\n",
      "[INFO] [12-08 12:39:06] data_source.py:563 [t:140481718310720]: start to download dataset zip from url https://bj.bcebos.com/easydata-upload/export_local/%E8%AF%84%E4%BC%B0%E4%BB%BB%E5%8A%A1_model_run_14x8azdccy_%E7%BB%93%E6%9E%9C%E9%9B%86_eeee55V1_20231208_123845.zip?authorization=bce-auth-v1%2F50c8bb753dcb4e1d8646bb1ffefd3503%2F2023-12-08T04%3A39%3A06Z%2F3600%2Fhost%2F5f6453dd8ccc7b239fd8487518b2356773c10e53e8437becef2549b8cbf15643\n",
      "[INFO] [12-08 12:39:06] data_source.py:582 [t:140481718310720]: download dataset zip to .qianfan_dataset_cache/32768/39815/1/bin.zip succeeded\n",
      "[INFO] [12-08 12:39:06] data_source.py:610 [t:140481718310720]: unzip dataset to path .qianfan_dataset_cache/32768/39815/1/content successfully\n",
      "[INFO] [12-08 12:39:06] data_source.py:614 [t:140481718310720]: write dataset info to path .qianfan_dataset_cache/32768/39815/1/info.json successfully\n",
      "[{\"source_entity_id\": \"9bd590018798b24ca3da4025b232543cb7c132d747b779a716c4e7315464ed66\", \"model_response\": [{\"model_version_id\": 12722, \"tag\": \"m_17320_9098>V1\", \"content\": \"结果涉及的地区是石化双雄（尤其是中石化）的炼油损逐月扩大、成品油销售受阻并且出现库存跌价。\"}], \"response\": [[\"从以上文本中我们暂无发现。\"]], \"prompt\": \"下文中市场价格下降导致销量（消费）减少事件对应的结果涉及的地区是？而发改委分别在5月10日和6月9日对国内汽柴油价格进行了下调，汽油和柴油零售价格分别累计下调了860和820元/吨，导致石化双雄（尤其是中石化）不仅炼油损逐月扩大、成品油销售受阻并且出现库存跌价\"}]\n",
      "[{\"source_entity_id\": \"3bff9181682936b0de808580d0d29b6f72f5227e8a9441eff73ee709bcd16322\", \"model_response\": [{\"model_version_id\": 12722, \"tag\": \"m_17320_9098>V1\", \"content\": \"无法推理出具体涉及的地区。\"}], \"response\": [[\"国内\"]], \"prompt\": \"下文中市场价格下降导致市场价格下降事件对应的结果涉及的地区是？在国际奶粉价格下降压力下,国内奶价仍有下降空间\"}]\n",
      "[{\"source_entity_id\": \"fc6708f44bc7e66fe802857a16c4ad947a1593161187a89ecd5054176914c505\", \"model_response\": [{\"model_version_id\": 12722, \"tag\": \"m_17320_9098>V1\", \"content\": \"因果事件：\\n- 原因：寒潮\\n- 结果：农产品价格继续攀升\"}], \"response\": [[\"寒潮导致市场价格提升\"]], \"prompt\": \"下文中有哪些因果事件？寒潮导致农产品价格继续攀升\"}]\n",
      "[{\"source_entity_id\": \"2d469d3fb6dea6e5ca6a1c7c0e91dceee6f918b495e22bedd1b496fc3f92147f\", \"model_response\": [{\"model_version_id\": 12722, \"tag\": \"m_17320_9098>V1\", \"content\": \"下文中需求增加导致市场价格提升事件对应的结果涉及的产品是高碳铬铁。\"}], \"response\": [[\"高碳铬铁\"]], \"prompt\": \"下文中需求增加导致市场价格提升事件对应的结果涉及的产品是？随着供应缩减，需求回升，高碳铬铁价格企稳回升，预期短期内或仍维持偏强态势，但高碳铬铁产能依然偏向过剩，后期价格弹升空间有限\"}]\n",
      "[{\"source_entity_id\": \"b7e4ea8632312142d853344214d34256d20a10d9fb890a81417a2ea9d2e70f04\", \"model_response\": [{\"model_version_id\": 12722, \"tag\": \"m_17320_9098>V1\", \"content\": \"根据上下文，需求增加导致市场价格上涨的事件涉及的产品是高碳铬铁。\"}], \"response\": [[\"高碳铬铁\"]], \"prompt\": \"下文中需求增加导致市场价格提升事件对应的原因涉及的产品是？随着供应缩减，需求回升，高碳铬铁价格企稳回升，预期短期内或仍维持偏强态势，但高碳铬铁产能依然偏向过剩，后期价格弹升空间有限\"}]\n",
      "[{\"source_entity_id\": \"6a3944e075cab0d524d9e7aac4e81048209e82c0fd68f5f43619d1f6b3820ffc\", \"model_response\": [{\"model_version_id\": 12722, \"tag\": \"m_17320_9098>V1\", \"content\": \"- 产品：煤炭\\n- 原因：煤炭价格大幅上涨\"}], \"response\": [[\"煤炭\"]], \"prompt\": \"下文中市场价格提升导致运营成本提升事件对应的原因涉及的产品是？利润和每股收益大幅下降的主要原因是煤炭价格大幅上涨，导致公司投资的火电企业的煤炭成本大幅增加###但报告期内受煤炭行业去产能改革的影响，公司煤炭前三季度平均采购价格同比上涨44.53%，导致成本涨幅抵消了销售价格的涨幅\"}]\n",
      "[{\"source_entity_id\": \"b8367703571f8e60b84c4cea5b27c82a212d3c58fb934f77588489641d7ec154\", \"model_response\": [{\"model_version_id\": 12722, \"tag\": \"m_17320_9098>V1\", \"content\": \"因果事件：\\n- 供求宽松：南美大豆供求宽松\\n- 价格：豆类油脂价格\\n\\n因此，因果事件可以表示为：南美大豆供求宽松对豆类油脂价格利空。\"}], \"response\": [[\"供给增加导致负向影响\"]], \"prompt\": \"下文中有哪些因果事件？南美大豆供求宽松对豆类油脂价格利空\"}]\n",
      "[{\"source_entity_id\": \"85b555f4d1b9562609c46dfbd99bf23d4b5a7993d3729de8e36b0d0b471367b4\", \"model_response\": [{\"model_version_id\": 12722, \"tag\": \"m_17320_9098>V1\", \"content\": \"- 产品：煤炭\\n- 利润和每股收益大幅下降的主要原因是：煤炭价格大幅上涨，导致公司投资的火电企业的煤炭成本大幅增加\"}], \"response\": [[\"煤炭\"]], \"prompt\": \"下文中市场价格提升导致运营成本提升事件对应的结果涉及的产品是？利润和每股收益大幅下降的主要原因是煤炭价格大幅上涨，导致公司投资的火电企业的煤炭成本大幅增加###但报告期内受煤炭行业去产能改革的影响，公司煤炭前三季度平均采购价格同比上涨44.53%，导致成本涨幅抵消了销售价格的涨幅\"}]\n",
      "[{\"source_entity_id\": \"4c22af1fc07c5fd051860c35c6cdd140a314a6657c3f223dc53e381acbb84d8b\", \"model_response\": [{\"model_version_id\": 12722, \"tag\": \"m_17320_9098>V1\", \"content\": \"下文中供给减少导致市场价格提升事件对应的结果涉及的产品是：\\n\\n* 原油价格大幅上涨\"}], \"response\": [[\"原油\"]], \"prompt\": \"下文中供给减少导致市场价格提升事件对应的结果涉及的产品是？东南亚棕榈油减产,原油价格大幅上涨###东南亚棕榈油减产,原油价格大幅上涨###东南亚棕榈油减产，原油价格大幅上涨###东南亚棕榈油减产，原油价格大幅上涨###东南亚棕榈油减产,原油价格大幅上涨###东南亚棕榈油减产，原油价格大幅上涨###东南亚棕榈油减产,原油价格大幅上涨###东南亚棕榈油减产,原油价格大幅上涨###东南亚棕榈油减产,原油价格大幅上涨\"}]\n",
      "[{\"source_entity_id\": \"5dc926ffd2c45cfad798c4a713ec55f471b107be9ff7a62dd1d234a62d8db11f\", \"model_response\": [{\"model_version_id\": 12722, \"tag\": \"m_17320_9098>V1\", \"content\": \"根据上下文描述，市场价格下降导致销量（消费）减少事件对应的原因涉及的产品是汽柴油。发改委在5月10日和6月9日对国内汽柴油价格进行了下调，导致石化双雄（尤其是中石化）不仅炼油损逐月扩大、成品油销售受阻并且出现库存跌价。因此，可以得出结论：汽柴油是导致市场价格下降导致销量（消费）减少事件对应的原因涉及的产品。\"}], \"response\": [[\"汽油,柴油\"]], \"prompt\": \"下文中市场价格下降导致销量（消费）减少事件对应的原因涉及的产品是？而发改委分别在5月10日和6月9日对国内汽柴油价格进行了下调，汽油和柴油零售价格分别累计下调了860和820元/吨，导致石化双雄（尤其是中石化）不仅炼油损逐月扩大、成品油销售受阻并且出现库存跌价\"}]\n",
      "[{\"source_entity_id\": \"32cc806d693c3e1990faa6062012387fc52d717e0bce757315ce9d780e2cfadc\", \"model_response\": [{\"model_version_id\": 12722, \"tag\": \"m_17320_9098>V1\", \"content\": \"- 产品：7-ACA\"}], \"response\": [[\"原料药\"]], \"prompt\": \"下文中市场价格下降导致市场价格下降事件对应的结果涉及的产品是？但由于7-ACA市场价格大幅下跌，跌幅超过50%，导致公司相关原料药销售价格随之下跌\"}]\n",
      "[{\"source_entity_id\": \"8c056bc81f6b5899a4144d155f9dfcb3d28e0807f966a2593f70f85dd7eaf989\", \"model_response\": [{\"model_version_id\": 12722, \"tag\": \"m_17320_9098>V1\", \"content\": \"南美大豆供求宽松对豆类油脂价格利空，这是因为南美大豆供应增加，导致全球大豆供应过剩，从而对豆类油脂价格产生负面影响。\"}], \"response\": [[\"南美\"]], \"prompt\": \"下文中供给增加导致负向影响事件对应的原因涉及的地区是？南美大豆供求宽松对豆类油脂价格利空\"}]\n",
      "[{\"source_entity_id\": \"40ad7274cca92e2b1fcec6c0c865d91be73258095251bbff36d831c550ff11d7\", \"model_response\": [{\"model_version_id\": 12722, \"tag\": \"m_17320_9098>V1\", \"content\": \"由于7-ACA市场价格大幅下跌，跌幅超过50%，导致公司相关原料药销售价格随之下跌。\"}], \"response\": [[\"暂不清楚，需要更多信息说明。\"]], \"prompt\": \"下文中市场价格下降导致市场价格下降事件对应的结果涉及的行业是？但由于7-ACA市场价格大幅下跌，跌幅超过50%，导致公司相关原料药销售价格随之下跌\"}]\n",
      "[{\"source_entity_id\": \"f63445156574736f818d80a217fffde44adc43b5732a11e68c0e92b5a75bcb37\", \"model_response\": [{\"model_version_id\": 12722, \"tag\": \"m_17320_9098>V1\", \"content\": \"东南亚棕榈油减产是导致原油价格大幅上涨的原因，因此涉及的地区是东南亚。\"}], \"response\": [[\"东南亚\"]], \"prompt\": \"下文中供给减少导致市场价格提升事件对应的原因涉及的地区是？东南亚棕榈油减产,原油价格大幅上涨###东南亚棕榈油减产,原油价格大幅上涨###东南亚棕榈油减产，原油价格大幅上涨###东南亚棕榈油减产，原油价格大幅上涨###东南亚棕榈油减产,原油价格大幅上涨###东南亚棕榈油减产，原油价格大幅上涨###东南亚棕榈油减产,原油价格大幅上涨###东南亚棕榈油减产,原油价格大幅上涨###东南亚棕榈油减产,原油价格大幅上涨\"}]\n",
      "[{\"source_entity_id\": \"659d72887b4d42de1fe10139572394aae52a956e2854c21d532e2bf0548b7d0c\", \"model_response\": [{\"model_version_id\": 12722, \"tag\": \"m_17320_9098>V1\", \"content\": \"风险因素：国内外因天气等自然灾害导致用肥量锐减。\\n\\n- 产品：肥料\"}], \"response\": [[\"肥\"]], \"prompt\": \"下文中其他自然灾害导致需求减少事件对应的结果涉及的产品是？风险因素:国内外因天气等自然灾害导致用肥量锐减\"}]\n",
      "[{\"source_entity_id\": \"95144fa1294e47937ceec5cf04d8d3bcdac8665dc9cbdc50f330ce622518e52b\", \"model_response\": [{\"model_version_id\": 12722, \"tag\": \"m_17320_9098>V1\", \"content\": \"产品：农产品\"}], \"response\": [[\"农产品\"]], \"prompt\": \"下文中寒潮导致市场价格提升事件对应的结果涉及的产品是？寒潮导致农产品价格继续攀升\"}]\n",
      "[{\"source_entity_id\": \"f06935306bce59d7f29e3c403161c5a72042b360b6d6f5dcdca0818f9e27840d\", \"model_response\": [{\"model_version_id\": 12722, \"tag\": \"m_17320_9098>V1\", \"content\": \"因果事件：\\n- 供应缩减：高碳铬铁价格企稳回升\\n- 需求回升：高碳铬铁价格企稳回升\\n- 高碳铬铁价格企稳回升：供应缩减、需求回升\\n- 产能过剩：高碳铬铁价格弹升空间有限\"}], \"response\": [[\"需求增加导致市场价格提升\"]], \"prompt\": \"下文中有哪些因果事件？随着供应缩减，需求回升，高碳铬铁价格企稳回升，预期短期内或仍维持偏强态势，但高碳铬铁产能依然偏向过剩，后期价格弹升空间有限\"}]\n",
      "[{\"source_entity_id\": \"18c81d2877b2609f8a5e7eb152703672839c4c1a5d85b6cbee95d2420cfc585f\", \"model_response\": [{\"model_version_id\": 12722, \"tag\": \"m_17320_9098>V1\", \"content\": \"产品：7-ACA。\"}], \"response\": [[\"7-ACA\"]], \"prompt\": \"下文中市场价格下降导致市场价格下降事件对应的原因涉及的产品是？但由于7-ACA市场价格大幅下跌，跌幅超过50%，导致公司相关原料药销售价格随之下跌\"}]\n",
      "[{\"source_entity_id\": \"ad33ea7a54d4f5ed394aa4e7051be4f0c0b650df44bd22e43948e1ffe64abe8e\", \"model_response\": [{\"model_version_id\": 12722, \"tag\": \"m_17320_9098>V1\", \"content\": \"因果事件：\\n- 原因：7-ACA市场价格大幅下跌\\n- 结果：公司相关原料药销售价格随之下跌\"}], \"response\": [[\"市场价格下降导致市场价格下降\"]], \"prompt\": \"下文中有哪些因果事件？但由于7-ACA市场价格大幅下跌，跌幅超过50%，导致公司相关原料药销售价格随之下跌\"}]\n",
      "[{\"source_entity_id\": \"acc26b5155cd9fbf88f7cd1a83cb325bf18669f6702d656143655d8b18501b9c\", \"model_response\": [{\"model_version_id\": 12722, \"tag\": \"m_17320_9098>V1\", \"content\": \"因果事件：\\n- 东南亚棕榈油减产：原油价格大幅上涨\"}], \"response\": [[\"供给减少导致市场价格提升\"]], \"prompt\": \"下文中有哪些因果事件？东南亚棕榈油减产,原油价格大幅上涨###东南亚棕榈油减产,原油价格大幅上涨###东南亚棕榈油减产，原油价格大幅上涨###东南亚棕榈油减产，原油价格大幅上涨###东南亚棕榈油减产,原油价格大幅上涨###东南亚棕榈油减产，原油价格大幅上涨###东南亚棕榈油减产,原油价格大幅上涨###东南亚棕榈油减产,原油价格大幅上涨###东南亚棕榈油减产,原油价格大幅上涨\"}]\n",
      "[{\"source_entity_id\": \"bcc35e9bacacdb6541591af21b36650c1f60efbe6f7fbe076c793ab5d5479fcc\", \"model_response\": [{\"model_version_id\": 12722, \"tag\": \"m_17320_9098>V1\", \"content\": \"因果事件：\\n- 因果事件：煤炭价格大幅上涨\\n\\t- 原因：利润和每股收益大幅下降\\n\\t- 原因：公司投资的火电企业的煤炭成本大幅增加\\n- 因果事件：煤炭行业去产能改革\\n\\t- 原因：公司煤炭前三季度平均采购价格同比上涨44.53%\\n\\t- 原因：成本涨幅抵消了销售价格的涨幅\"}], \"response\": [[\"市场价格提升导致运营成本提升\"]], \"prompt\": \"下文中有哪些因果事件？利润和每股收益大幅下降的主要原因是煤炭价格大幅上涨，导致公司投资的火电企业的煤炭成本大幅增加###但报告期内受煤炭行业去产能改革的影响，公司煤炭前三季度平均采购价格同比上涨44.53%，导致成本涨幅抵消了销售价格的涨幅\"}]\n",
      "[{\"source_entity_id\": \"43d24d918e75bde32dcc9e6e4c00cc549242950404c744c790b7f39669be3bd3\", \"model_response\": [{\"model_version_id\": 12722, \"tag\": \"m_17320_9098>V1\", \"content\": \"没有因果事件可以返回。\"}], \"response\": [[\"其他自然灾害导致需求减少\"]], \"prompt\": \"下文中有哪些因果事件？风险因素:国内外因天气等自然灾害导致用肥量锐减\"}]\n",
      "[{\"source_entity_id\": \"6b641a4720836a260300bbe745401d30084ff355ad2ff50cf261fed0de9e3622\", \"model_response\": [{\"model_version_id\": 12722, \"tag\": \"m_17320_9098>V1\", \"content\": \"豆类油脂产品。\"}], \"response\": [[\"大豆\"]], \"prompt\": \"下文中供给增加导致负向影响事件对应的原因涉及的产品是？南美大豆供求宽松对豆类油脂价格利空\"}]\n",
      "[{\"source_entity_id\": \"72c11bbe986dfed3313553920252e25623de875247c6c639573c75acaf6fbf6a\", \"model_response\": [{\"model_version_id\": 12722, \"tag\": \"m_17320_9098>V1\", \"content\": \"豆类油脂产品。\\n\\n题干中提到南美大豆供求宽松对豆类油脂价格产生负向影响，因此可以推断出该事件涉及的产品是豆类油脂产品。\"}], \"response\": [[\"豆类油脂\"]], \"prompt\": \"下文中供给增加导致负向影响事件对应的结果涉及的产品是？南美大豆供求宽松对豆类油脂价格利空\"}]\n",
      "[{\"source_entity_id\": \"a7fc56ddd546dc70bcf3692a5d2ae71eecb30a5bd244d7f0164e46423a00decc\", \"model_response\": [{\"model_version_id\": 12722, \"tag\": \"m_17320_9098>V1\", \"content\": \"根据上文描述，煤炭价格大幅上涨是导致公司投资的火电企业的煤炭成本大幅增加的原因，因此涉及的行业是煤炭行业。\"}], \"response\": [[\"以上内容暂无说明。\"]], \"prompt\": \"下文中市场价格提升导致运营成本提升事件对应的原因涉及的行业是？利润和每股收益大幅下降的主要原因是煤炭价格大幅上涨，导致公司投资的火电企业的煤炭成本大幅增加###但报告期内受煤炭行业去产能改革的影响，公司煤炭前三季度平均采购价格同比上涨44.53%，导致成本涨幅抵消了销售价格的涨幅\"}]\n",
      "[{\"source_entity_id\": \"0b73b52d54e57a1f876bfcc2546bda42271f97966f57363c2f7315056d0a19d9\", \"model_response\": [{\"model_version_id\": 12722, \"tag\": \"m_17320_9098>V1\", \"content\": \"- 产品：奶粉\"}], \"response\": [[\"奶\"]], \"prompt\": \"下文中市场价格下降导致市场价格下降事件对应的结果涉及的产品是？在国际奶粉价格下降压力下,国内奶价仍有下降空间\"}]\n",
      "[{\"source_entity_id\": \"a814c1dcd1efb99fb6b93bdcaf256d3b6772165bdff45bb9001b239aa63f7828\", \"model_response\": [{\"model_version_id\": 12722, \"tag\": \"m_17320_9098>V1\", \"content\": \"结果：\\n- 产品：成品油\\n\\n事件：\\n- 发生时间：5月10日、6月9日\\n- 降价主体：发改委\\n- 降价对象：国内汽柴油价格\\n- 降价幅度：860元/吨、820元/吨\\n- 影响：石化双雄（尤其是中石化）不仅炼油损逐月扩大、成品油销售受阻并且出现库存跌价\"}], \"response\": [[\"成品油\"]], \"prompt\": \"下文中市场价格下降导致销量（消费）减少事件对应的结果涉及的产品是？而发改委分别在5月10日和6月9日对国内汽柴油价格进行了下调，汽油和柴油零售价格分别累计下调了860和820元/吨，导致石化双雄（尤其是中石化）不仅炼油损逐月扩大、成品油销售受阻并且出现库存跌价\"}]\n",
      "[{\"source_entity_id\": \"2b725fe7379b039ca9b1056a0dbf8eca674762afcc1ba42bf2068b7488aec94f\", \"model_response\": [{\"model_version_id\": 12722, \"tag\": \"m_17320_9098>V1\", \"content\": \"根据所提供的信息，事件的原因涉及产品是“棕榈油”。\"}], \"response\": [[\"棕榈油\"]], \"prompt\": \"下文中供给减少导致市场价格提升事件对应的原因涉及的产品是？东南亚棕榈油减产,原油价格大幅上涨###东南亚棕榈油减产,原油价格大幅上涨###东南亚棕榈油减产，原油价格大幅上涨###东南亚棕榈油减产，原油价格大幅上涨###东南亚棕榈油减产,原油价格大幅上涨###东南亚棕榈油减产，原油价格大幅上涨###东南亚棕榈油减产,原油价格大幅上涨###东南亚棕榈油减产,原油价格大幅上涨###东南亚棕榈油减产,原油价格大幅上涨\"}]\n",
      "[{\"source_entity_id\": \"06afea01907cf40a7ec72e53bf0a35f7763ad388ca6c5a1eacf20c3d28212879\", \"model_response\": [{\"model_version_id\": 12722, \"tag\": \"m_17320_9098>V1\", \"content\": \"很抱歉，文中没有提及下文中其他自然灾害导致需求减少事件对应的原因涉及的地区是。\"}], \"response\": [[\"国内外\"]], \"prompt\": \"下文中其他自然灾害导致需求减少事件对应的原因涉及的地区是？风险因素:国内外因天气等自然灾害导致用肥量锐减\"}]\n",
      "[{\"source_entity_id\": \"0efd232c1731a2ab98212612164103a6ba9d3c55a8032abf849a79f1478bd18a\", \"model_response\": [{\"model_version_id\": 12722, \"tag\": \"m_17320_9098>V1\", \"content\": \"很抱歉，文中没有提及下文中其他自然灾害导致需求减少事件对应的原因涉及的产品。\"}], \"response\": [[\"以上内容暂无说明。\"]], \"prompt\": \"下文中其他自然灾害导致需求减少事件对应的原因涉及的产品是？风险因素:国内外因天气等自然灾害导致用肥量锐减\"}]\n",
      "[{\"source_entity_id\": \"dfdca634a8cfdfa1053174c25cebd2800165e3956f52df492f423d17623c4149\", \"model_response\": [{\"model_version_id\": 12722, \"tag\": \"m_17320_9098>V1\", \"content\": \"因果事件：\\n- 原因：发改委分别在5月10日和6月9日对国内汽柴油价格进行了下调\\n- 结果：汽油和柴油零售价格分别累计下调了860和820元/吨，导致石化双雄（尤其是中石化）不仅炼油损逐月扩大、成品油销售受阻并且出现库存跌价\"}], \"response\": [[\"市场价格下降导致销量（消费）减少\"]], \"prompt\": \"下文中有哪些因果事件？而发改委分别在5月10日和6月9日对国内汽柴油价格进行了下调，汽油和柴油零售价格分别累计下调了860和820元/吨，导致石化双雄（尤其是中石化）不仅炼油损逐月扩大、成品油销售受阻并且出现库存跌价\"}]\n",
      "[{\"source_entity_id\": \"e90befb7ed6735ec6a196beee7bfbe160ce044e7de04cb8c540aae7583657ea0\", \"model_response\": [{\"model_version_id\": 12722, \"tag\": \"m_17320_9098>V1\", \"content\": \"根据提供的信息，需求回升是导致高碳铬铁价格企稳回升的原因。因此，可以推断出该事件涉及的行业是钢铁行业。\"}], \"response\": [[\"我们无法得知，可能需要更多内容说明。\"]], \"prompt\": \"下文中需求增加导致市场价格提升事件对应的原因涉及的行业是？随着供应缩减，需求回升，高碳铬铁价格企稳回升，预期短期内或仍维持偏强态势，但高碳铬铁产能依然偏向过剩，后期价格弹升空间有限\"}]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from qianfan.model import Model\n",
    "from qianfan.dataset import Dataset\n",
    "\n",
    "# 首先需要先加载测试数据集，这里以加载平台预置数据集为例子：\n",
    "test_ds = Dataset.load(qianfan_dataset_id=\"xxx\", is_download_to_local=False)\n",
    "\n",
    "# 从训练结果中获取模型对象\n",
    "m: Model = trainer.output[\"model\"]\n",
    "\n",
    "# 运行批量任务获取结果数据集\n",
    "result_ds: Dataset = m.batch_inference(test_ds)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "通过这种方式运行完成后，可以直接在本地拿到一份批量运行的结果，我们可以通过dataset.list查看其中的部分数据："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[{'model_response': [{'content': '下文中需求增加导致市场价格提升事件对应的结果涉及的产品是高碳铬铁。',\n",
       "     'model_version_id': 12722,\n",
       "     'tag': 'm_17320_9098>V1'}],\n",
       "   'prompt': '下文中需求增加导致市场价格提升事件对应的结果涉及的产品是？随着供应缩减，需求回升，高碳铬铁价格企稳回升，预期短期内或仍维持偏强态势，但高碳铬铁产能依然偏向过剩，后期价格弹升空间有限',\n",
       "   'response': [['高碳铬铁']],\n",
       "   'source_entity_id': '2d469d3fb6dea6e5ca6a1c7c0e91dceee6f918b495e22bedd1b496fc3f92147f'}],\n",
       " [{'model_response': [{'content': '根据上下文，需求增加导致市场价格上涨的事件涉及的产品是高碳铬铁。',\n",
       "     'model_version_id': 12722,\n",
       "     'tag': 'm_17320_9098>V1'}],\n",
       "   'prompt': '下文中需求增加导致市场价格提升事件对应的原因涉及的产品是？随着供应缩减，需求回升，高碳铬铁价格企稳回升，预期短期内或仍维持偏强态势，但高碳铬铁产能依然偏向过剩，后期价格弹升空间有限',\n",
       "   'response': [['高碳铬铁']],\n",
       "   'source_entity_id': 'b7e4ea8632312142d853344214d34256d20a10d9fb890a81417a2ea9d2e70f04'}],\n",
       " [{'model_response': [{'content': '- 产品：煤炭\\n- 原因：煤炭价格大幅上涨',\n",
       "     'model_version_id': 12722,\n",
       "     'tag': 'm_17320_9098>V1'}],\n",
       "   'prompt': '下文中市场价格提升导致运营成本提升事件对应的原因涉及的产品是？利润和每股收益大幅下降的主要原因是煤炭价格大幅上涨，导致公司投资的火电企业的煤炭成本大幅增加###但报告期内受煤炭行业去产能改革的影响，公司煤炭前三季度平均采购价格同比上涨44.53%，导致成本涨幅抵消了销售价格的涨幅',\n",
       "   'response': [['煤炭']],\n",
       "   'source_entity_id': '6a3944e075cab0d524d9e7aac4e81048209e82c0fd68f5f43619d1f6b3820ffc'}]]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "result_ds.list([i for i in range(3,6)])"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在完成模型的批量运行后，我们可以对模型有一个简单的体感评估，如果效果不错，我们可以选择发布成服务以最终应用生产："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[INFO] [12-08 12:40:10] model.py:497 [t:140481718310720]: ready to deploy service with model 10268/12722\n",
      "[INFO] [12-08 12:40:18] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 12:40:52] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 12:41:22] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 12:41:53] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 12:42:24] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 12:42:54] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 12:43:25] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 12:43:55] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 12:44:26] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 12:44:57] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 12:45:27] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 12:45:58] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 12:46:28] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 12:47:01] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 12:47:32] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 12:48:02] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 12:48:33] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 12:49:04] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 12:49:34] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 12:50:05] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 12:50:36] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 12:51:06] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 12:51:37] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 12:52:07] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 12:52:38] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 12:53:09] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 12:53:40] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 12:54:12] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 12:54:42] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 12:55:13] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 12:55:43] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 12:56:14] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 12:56:45] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 12:57:15] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 12:57:46] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 12:58:16] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 12:58:47] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 12:59:18] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 12:59:48] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:00:19] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:00:51] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:01:22] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:01:52] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:02:23] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:02:53] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:03:24] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:03:54] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:04:25] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:04:56] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:05:27] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:05:57] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:06:28] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:06:58] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:07:30] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:08:01] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:08:33] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:09:04] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:09:35] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:10:07] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:10:38] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:11:11] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:11:43] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:12:14] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:12:46] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:13:17] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:13:48] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:14:20] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:14:52] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:15:24] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:15:55] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:16:26] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:16:58] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:17:29] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:18:02] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:18:34] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:19:05] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:19:36] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:20:08] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:20:39] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:21:12] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:21:43] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:22:14] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:22:46] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:23:17] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:23:49] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:24:21] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:24:53] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:25:24] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:25:55] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:26:27] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:26:58] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:27:29] model.py:529 [t:140481718310720]: please check web console `https://console.bce.baidu.com/qianfan/ais/console/onlineService`,for service  deployment payment.\n",
      "[INFO] [12-08 13:28:03] model.py:536 [t:140481718310720]: service 5093 has been deployed in `/ex1ndqpo_sdkcqa1` \n"
     ]
    }
   ],
   "source": [
    "from qianfan.model import Service\n",
    "from qianfan.model.consts import ServiceType\n",
    "from qianfan.resources.console.consts import DeployPoolType\n",
    "\n",
    "sft_svc: Service = m.deploy(DeployConfig(\n",
    "    name=\"sdkcqasvc\",\n",
    "    endpoint_prefix=\"sdkcqa1\",\n",
    "    replicas=1, # 副本数， 与qps强绑定\n",
    "    pool_type=DeployPoolType.PrivateResource, # 私有资源池\n",
    "    service_type=ServiceType.Chat,\n",
    "))\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用Finetune之后的模型服务和原始的预置模型服务调用："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[INFO] [12-08 13:30:55] openapi_requestor.py:134 [t:140481718310720]: requesting llm api endpoint: /chat/ex1ndqpo_sdkcqa1\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'因果事件如下：\\n- 因果事件：无取向硅钢产量的持续提升导致市场竞争愈发激烈，价格进一步降低，从而有效减少新能源汽车驱动电机行业内企业的成本支出'"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from qianfan import ChatCompletion\n",
    "### 使用Model & Service调用模型\n",
    "\n",
    "problem=\"下文中有哪些因果事件？无取向硅钢广泛应用于铁芯等电机零部件其产量的持续提升导致市场竞争愈发激烈，价格进一步降低，从而有效减少新能源汽车驱动电机行业内企业的成本支出\"\n",
    "\n",
    "#获取服务对象，即ChatCompletion等类型的对象\n",
    "chat_comp: ChatCompletion = sft_svc.get_res()\n",
    "sft_chat_resp = chat_comp.do(messages=[{\"content\": problem, \"role\": \"user\"}])\n",
    "sft_chat_resp[\"result\"]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "58f7cb64c3a06383b7f18d2a11305edccbad427293a2b4afa7abe8bfc810d4bb"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
